package Leetcode;

import java.util.LinkedList;

public class problem_239 {

    public static int[] maxSlidingWindow(int[] nums, int k) {
        int[] res = new int[nums.length - k + 1];
        if(k==0||nums.length==0){
            return nums;
        }
        LinkedList<Integer> list = new LinkedList<>();
        for( int i = 0; i < nums.length ; i++){
            while( !list.isEmpty() && nums[list.peekLast()] < nums[i]){
                list.pollLast();
            }
            list.addLast(i);
            if(list.peekFirst()==i-k){
                list.pollFirst();
            }
            if(i-k>=-1){
                res[i-k+1]=nums[list.peekFirst()];
            }
        }

        return res;

    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,3,-1,-3,5,3,6,7};
        maxSlidingWindow(nums,3);
    }

}
